package com.zhuangfei.hpuhelper.view;

import java.io.File;

import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.service.LoginService;
import com.hpu.service.ShareUploadService;
import com.hpu.tools.ShareTools;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhuangfei.hpuhelper.bean.ShareDataBean;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.tools.ViewTools;
import com.zhuangfei.toolkit.tools.ToastTools;

import cn.lzf.serviceplatform.R;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class ShareDetailActivity extends Activity {

    private LinearLayout shareContainer;
    private ImageView shareImageView;

    private ShareUploadService shareUploadService;
    private LoginService loginService;

    private boolean isLoad = false;
    private LinearLayout backLayout;

    String json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_detail);
        initView();
        initData();
    }

    private void initData() {
        json = getIntent().getStringExtra("json");
        if(!TextUtils.isEmpty(json)){
            ToastTools.show(this,"待分享课程为空");
        }else{
            shareUploadService.doUpload(json);
        }
    }

    private void initView() {
        shareContainer = (LinearLayout) findViewById(R.id.id_manager_share_container);
        shareImageView = (ImageView) findViewById(R.id.id_manager_image);
        backLayout = (LinearLayout) findViewById(R.id.id_back);

        shareUploadService = new ShareUploadService(handler, this);
        loginService = new LoginService(this,handler);

        backLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                goBack();
            }
        });
    }

    protected void goBack() {
        ActivityTools.toBackActivityAnim(this, CourseManagerActivity.class);
    }

    DefaultHandler handler = new DefaultHandler(this) {
        public void handleMessage(android.os.Message msg) {
            Log.i("Msg", msg.what + "");
            super.handleMessage(msg);

            switch (msg.what) {
                case HttpConstants.SHARE_COURSE_UPLOAD_SUCCESS:
                    String id = msg.obj.toString();
                    if (id.length() > 60)
                        Toast.makeText(ShareDetailActivity.this, "返回值异常，请重试!", Toast.LENGTH_SHORT).show();
                    else {
                        Bitmap logo = drawableToBitmap(getResources().getDrawable(R.drawable.app));
                        Bitmap bitmap = CodeUtils.createImage("$course$" + id, 400, 400, logo);
                        shareImageView.setImageBitmap(bitmap);
                        isLoad = true;
                    }
                    break;
                case HttpConstants.VPN_TOOLONG:
                    if(handler.isIgnoreVpnExpired()){
                        break;
                    }
                    loginService.login(ShareTools.get(ShareDetailActivity.this, ShareConstants.STORE_USER, "").toString(),
                            ShareTools.get(ShareDetailActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
                            ShareTools.get(ShareDetailActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
                    break;

                // 登录成功
                case HttpConstants.LOGIN_SUCCESS:
                    ShareTools.put(ShareDetailActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
                    shareUploadService.doUpload(json);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    public void onShare(View view) {
        if (isLoad) {
            File myFile = ViewTools.viewSaveToImage(shareContainer, "shareview_" + System.currentTimeMillis() + ".png");
            shareImage(myFile);
        } else {
            Toast.makeText(this, "出现错误,请重新进入!", Toast.LENGTH_SHORT).show();
        }

    }

    public void shareImage(File file) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("image/jpg");
        Uri u = Uri.fromFile(file);
        shareIntent.putExtra(Intent.EXTRA_STREAM, u);

        // 设置分享列表的标题，并且每次都显示分享列表
        startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

    public Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        // canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    @Override
    public void onBackPressed() {
        goBack();
    }
}
